package com.example.polarity;

import android.R.interpolator;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;

public class Planet extends View {

	//need to set it up such that we get the position of the sun and base the rotation on that
	//also need to set the pivot points relative to the y x axis as its curently not set that way just a hard coded value
	
	private float pivotX =150;
	private float pivotY = 20;
	private int duration = 4000;
	
	public Planet(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}
	
	public Planet(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		
		for(int i = 0; i < 8; i++)
		{
			String planet = "Planet" + i;
			
			if(this.getTag().equals(planet))
			{
				this.duration = this.duration * i;
			}
		}
		
		

		
		// TODO Auto-generated constructor stub
	}
	
	public Planet(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		

		// TODO Auto-generated constructor stub
	}
	@Override
	public void onLayout(boolean a, int b, int c, int d, int e)
	{
		DisplayMetrics metrics = this.getContext().getResources().getDisplayMetrics();
		int ScreenWidth = metrics.widthPixels;
		
		if(!this.getTag().equals("Sun"))
		{
			this.pivotX = (ScreenWidth - this.getLeft()) - 150;
			Animation an = new RotateAnimation(0.0f, 360.0f, pivotX, pivotY);

		    // Set the animation's parameters
		
		    an.setDuration(duration);               // duration in ms
		    an.setRepeatCount(-1);   			// -1 = infinite repeated
		    an.setFillBefore(true);
		    an.setFillEnabled(true);
		    an.setFillAfter(true);               // keep rotation after animation
		    an.setInterpolator(this.getContext(), interpolator.linear);
		 
		    this.setAnimation(an);
		}
		
		if(this.getTag().equals("Sun"))
		{
			 	Animation an = new RotateAnimation(0.0f, 359.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);  //new RotateAnimation(0.0f, 360.0f, this.getWidth(), this.getHeight());

			    // Set the animation's parameters
			
			    an.setDuration(8000);               // duration in ms
			    an.setRepeatCount(-1);   			// -1 = infinite repeated
			    an.setFillBefore(true);
			    an.setFillEnabled(true);
			    an.setFillAfter(true);               // keep rotation after animation
			    an.setInterpolator(this.getContext(), interpolator.linear);
			 
			 this.setAnimation(an);
		}
	}
	
	@Override 
	public void onDraw(Canvas canvas)
	{
	}

}
